package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserinfoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserinfoRedisService redisService;

    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @GetMapping("/get")
    public Object get(String id) {
        return userInfoService.get(id);
    }

    @GetMapping("/checkPhone")
    public Object checkPhone(String phone) {
        boolean ble = userInfoService.findByPhone(phone);
        return ble;
    }

    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone) {
        userInfoService.sendMessages(phone);
        return JsonResult.success();
    }

    /**
     * phone: 1330000000
     * nickname: zhuguoxi
     * password: 123
     * rpassword: 123
     * verifyCode: 1b51
     *
     * @param phone
     * @return
     */
    @PostMapping("/regist")
    public Object regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        userInfoService.register(phone, nickname, password, rpassword, verifyCode);
        return JsonResult.success();
    }

    @PostMapping("/login")
    public Object login(String username, String password) {
        UserInfo userInfo = userInfoService.login(username, password);
        String token = redisService.setToken(userInfo);
        System.out.println(userInfo);
        Map<String, Object> map = new HashMap<>();
        map.put("user", userInfo);
        map.put("token", token);
        return JsonResult.success(map);
    }

    @GetMapping("/currentUser")
    @RequireLogin
    public Object currentUser(HttpServletRequest request) {
        String token = request.getHeader("token");
        UserInfo userInfo = redisService.getToken(token);
        return JsonResult.success(userInfo);
    }

    @GetMapping("/users")
    public Object users(@UserParam UserInfo userInfo) {
        return JsonResult.success(userInfo);
    }

    @GetMapping("/strategies/favor")
    public Object favor(String sid, String userId) {
        List<String> list=strategyStatisVORedisService.queryFavorNum(sid, userId);
        return JsonResult.success(list);
    }
}
